package com.hankol.winecellar.viewmodel

import android.content.Context
import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.Constants
import com.baselibrary.base.BasicListBean
import com.baselibrary.base.mvvm.BaseViewModel
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.hankol.newretail.bean.GoodListBean
import com.hankol.winecellar.adapter.EditWineryUploadImageVideoAdapter
import com.hankol.winecellar.adapter.UploadImageVideoAdapter
import com.hankol.winecellar.bean.*
import com.hankol.winecellar.databinding.ActivityEditOnlineWinecellarBinding
import com.hankol.winecellar.databinding.ActivityWineCellarDetailBinding
import com.hankol.winecellar.respository.WineRequestManager

/**
 * @author: merbng
 * @time: 2021/10/15 10:05
 * @description:
 */
class WineCellarViewModel : BaseViewModel() {
    var wineListLD: MutableLiveData<BasicListBean<NearbyWineCellarListBean>> = MutableLiveData()
    var wineryInfoDataLD: MutableLiveData<List<WineryInfoDataBean>> = MutableLiveData()
    var wineryWineryListDataLD: MutableLiveData<List<WineryInfoDataBean>> = MutableLiveData()
    var configListLD: MutableLiveData<MineCenterMenuBean> = MutableLiveData()
    var userManageInfoLD: MutableLiveData<WineryManageInfoDataBean> = MutableLiveData()
    var evaluateListLD: MutableLiveData<BasicListBean<WineryEvaluateListBean>> = MutableLiveData()
    var privateMealListLD: MutableLiveData<BasicListBean<PrivateMealManageListBean>> = MutableLiveData()
    var wineryDataInfoLD: MutableLiveData<WineryDataInfoBean> = MutableLiveData()
    var wineryManagerLD: MutableLiveData<WineryManagerBean> = MutableLiveData()
    var changeWineryManagerStatusLD: MutableLiveData<Boolean> = MutableLiveData()
    var addCommentLD: MutableLiveData<Boolean> = MutableLiveData()
    var deleteCommentLD: MutableLiveData<Boolean> = MutableLiveData()
    var publishMealLD: MutableLiveData<Boolean> = MutableLiveData()
    var deletePrivateMealLD: MutableLiveData<Boolean> = MutableLiveData()
    var createEditWinery: MutableLiveData<Boolean> = MutableLiveData()
    var isManagerLD: MutableLiveData<Boolean> = MutableLiveData()
    var uploadGetUrlBatchLD: MutableLiveData<List<String>> = MutableLiveData()
    var privateMealImgPath: MutableLiveData<String> = MutableLiveData()

    //限时秒杀
    var seckillListLD: MutableLiveData<BasicListBean<WineryGoodsSeckillBean>> = MutableLiveData()

    //特色标签
    var labelCongigLD4Special: MutableLiveData<List<LabelConfigFacilityBean>> = MutableLiveData()
    var labelCongigLD4Base: MutableLiveData<List<LabelConfigFacilityBean>> = MutableLiveData()
    lateinit var mBinding: ActivityEditOnlineWinecellarBinding
    var goodsManagerListLD: MutableLiveData<BasicListBean<GoodsManagerListBean>> = MutableLiveData()

    fun setItemHelper(rvImagevideo: RecyclerView, mTouchHelper: ItemTouchHelper, mImageVideoAdapter: UploadImageVideoAdapter) {
        if (mImageVideoAdapter.listData.size > 1) {
            mTouchHelper.attachToRecyclerView(rvImagevideo)
            mImageVideoAdapter.setItemHelper(mTouchHelper)
        } else {
            mTouchHelper.attachToRecyclerView(null)
            mImageVideoAdapter.setItemHelper(null)
        }
    }

    fun setItemHelper2EditWinery(rvImagevideo: RecyclerView, mTouchHelper: ItemTouchHelper, mImageVideoAdapter: EditWineryUploadImageVideoAdapter) {
        if (mImageVideoAdapter.listData.checkNotNull() && mImageVideoAdapter.listData.size > 1) {
            mTouchHelper.attachToRecyclerView(rvImagevideo)
            mImageVideoAdapter.setItemHelper(mTouchHelper)
        } else {
            mTouchHelper.attachToRecyclerView(null)
            mImageVideoAdapter.setItemHelper(null)
        }
    }


    //全部评价列表
    fun requestEvaluateList(wineryCode: String, pageNum: Int, size: Int = 20) {
        WineRequestManager.getInstance().requestWineryAllEvaluateList(evaluateListLD, wineryCode, pageNum, size)
    }

    //获取经营者所有酒窖列表
    fun reqgetWineryList(phone: String, dealerCode: String) {
        WineRequestManager.getInstance().reqgetWineryList(phone, dealerCode, wineryInfoDataLD)
    }

    //是否经营者模式
    fun reqIsWineryManager() {
        WineRequestManager.getInstance().reqIsWineryManager(isManagerLD)
    }

    //获取经营者所有酒窖列表 弹窗
    fun reqgetWineryList(phone: String) {
        WineRequestManager.getInstance().reqgetWineryList(phone, "", wineryWineryListDataLD)
    }

    //经营者中心-功能列表【路由】
    fun reqConfigCenterList(dealerCode: String) {
        WineRequestManager.getInstance().reqConfigCenterList(dealerCode, configListLD)
    }

    //获取经营者信息
    fun reqWineryDealerInfo(dealerCode: String) {
        WineRequestManager.getInstance().reqWineryDealerInfo(dealerCode, userManageInfoLD)
    }

    /**
     *编辑酒窖数据回显
     * wineryId:线上酒窖编号
     * type 0编辑 1预览
     * 附近酒窖列表进入 传type 0
     */
    fun reqgetWineryDataInfo(type: Int, wineryCode: String, dealerCode: String) {
        WineRequestManager.getInstance().reqgetWineryDataInfo(type, wineryCode, dealerCode, wineryDataInfoLD)
    }

    //私房菜管理 列表
    fun reqPrivateMealList(wineryCode: String, page: Int, size: Int = 10) {
        WineRequestManager.getInstance().reqPrivateMealList(wineryCode, page, size, privateMealListLD)
    }

    //酒窖内限时秒杀 列表
    fun reqSeckillList(shopId: Int, page: Int, size: Int = 5) {
        WineRequestManager.getInstance().reqSeckillList(shopId, page, size, seckillListLD)
    }

    //添加 编辑 评论
    fun reqAddOrEditComment(isEdit: Boolean = false, commentId: Long, wineryCommentDesc: String, wineryCommentImg: String, wineryName: String) {
        WineRequestManager.getInstance().reqAddComment(addCommentLD, isEdit, commentId, wineryCommentDesc, wineryCommentImg, wineryName)
    }

    //删除评论
    fun reqDeleteComment(id: Long) {
        WineRequestManager.getInstance().reqDeleteComment(deleteCommentLD, id)
    }


    //发布下酒菜
    fun reqAddPrivateMeal(appetizerDesc: String, appetizerImg: String, appetizerName: String, sort: Int) {
        WineRequestManager.getInstance().reqAddPrivateMeal(publishMealLD, appetizerDesc, appetizerImg, appetizerName, sort)
    }

    //编辑下酒菜
    fun reqEditPrivateMeal(id: Int, appetizerDesc: String, appetizerImg: String, appetizerName: String, sort: Int) {
        WineRequestManager.getInstance().reqEditPrivateMeal(publishMealLD, id, appetizerDesc, appetizerImg, appetizerName, sort)
    }

    //删除下酒菜
    fun reqDeletePrivateMeal(id: Int) {
        WineRequestManager.getInstance().reqDeletePrivateMeal(deletePrivateMealLD, id)
    }

    //基础设施列表  type： 0 特色标签 1基础设施标签
    fun reqgetWineryLabelConfig4Special() {
        WineRequestManager.getInstance().reqgetWineryLabelConfig(labelCongigLD4Special, 0)
    }

    //基础设施列表  type： 0 特色标签 1基础设施标签
    fun reqgetWineryLabelConfig4Base() {
        WineRequestManager.getInstance().reqgetWineryLabelConfig(labelCongigLD4Base, 1)
    }

    //获取线上酒窖管理数据
    fun reqgetWineryManageData() {
        WineRequestManager.getInstance().reqgetWineryManageData(wineryManagerLD)
    }

    //修改经营者状态 0未选择 1社区 2经营者
    fun reqChangeWineryManagerStatus(wineryStatus: Int) {
        WineRequestManager.getInstance().reqChangeWineryManagerStatus(wineryStatus, changeWineryManagerStatusLD)
    }

    //创建/编辑酒窖数据
    fun reqEditWinery(wineryImg: String, winerySlideshow: String, consumptionPrice: String, startTime: String, endTime: String, wineryDealerPhone: String, wineryDesc: String, wineryFacility: String, wineryFeature: String) {
        WineRequestManager.getInstance().reqEditWinery(createEditWinery, wineryImg, winerySlideshow, consumptionPrice, startTime, endTime, wineryDealerPhone, wineryDesc, wineryFacility, wineryFeature)
    }
/*----------------------------------海报分享相关start------------------------------------------*/

    fun upSubTitle(context: Context, y: Int, binding: ActivityWineCellarDetailBinding) {
        val scrollY = y - ScreenUtils.getStatusBarHeight(context)
        if (scrollY <= binding.llScrollStoreGoods.top && binding.llScrollStoreGoods.visibility == View.VISIBLE) {
            //选中店内好货
            boldShowTag(binding.tvSubTitleStoreGoods, binding.imgStoreGoodsTag, true)
            boldShowTag(binding.tvSubTitleMeal, binding.imgMealTag, false)
            boldShowTag(binding.tvSubTitleComment, binding.imgCommentTag, false)
            boldShowTag(binding.tvSubTitleDynamic, binding.imgDynamicTag, false)
        } else if (scrollY > binding.llScrollStoreGoods.top && scrollY >= binding.llScrollMeal.top && scrollY < binding.llScrollComment.top && binding.llScrollMeal.visibility == View.VISIBLE) {
            //选中下酒菜
            boldShowTag(binding.tvSubTitleStoreGoods, binding.imgStoreGoodsTag, false)
            boldShowTag(binding.tvSubTitleMeal, binding.imgMealTag, true)
            boldShowTag(binding.tvSubTitleComment, binding.imgCommentTag, false)
            boldShowTag(binding.tvSubTitleDynamic, binding.imgDynamicTag, false)
        } else if (scrollY > binding.llScrollMeal.top && scrollY >= binding.llScrollComment.top && scrollY < binding.llScrollDynamic.top && binding.llScrollComment.visibility == View.VISIBLE) {
            //选中评论
            boldShowTag(binding.tvSubTitleStoreGoods, binding.imgStoreGoodsTag, false)
            boldShowTag(binding.tvSubTitleMeal, binding.imgMealTag, false)
            boldShowTag(binding.tvSubTitleComment, binding.imgCommentTag, true)
            boldShowTag(binding.tvSubTitleDynamic, binding.imgDynamicTag, false)
        } else if (scrollY >= binding.llScrollDynamic.top && binding.llScrollDynamic.visibility == View.VISIBLE) {
            //选中动态
            boldShowTag(binding.tvSubTitleStoreGoods, binding.imgStoreGoodsTag, false)
            boldShowTag(binding.tvSubTitleMeal, binding.imgMealTag, false)
            boldShowTag(binding.tvSubTitleComment, binding.imgCommentTag, false)
            boldShowTag(binding.tvSubTitleDynamic, binding.imgDynamicTag, true)
        }
    }


    fun boldShowTag(tv: TextView, imgTag: ImageView, isSelect: Boolean) {
        if (isSelect) {
            //加粗
            tv.typeface = Typeface.defaultFromStyle(Typeface.BOLD);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18f)
            imgTag.visibility = View.VISIBLE
        } else {
            tv.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16f)
            imgTag.visibility = View.INVISIBLE
        }
    }

    /**
     * 创建bnner数据
     */
    fun createBanner(coversLooperList: ArrayList<WineryCoverBean>): ArrayList<String> {
        val bannerList: ArrayList<String> = arrayListOf()
        coversLooperList.forEach {
            //图片视频都会有coverUrl
            bannerList.add(it.coverUrl)
//            if (it.type == 0) {
//                bannerList.add(it.coverUrl)
//            } else {
//                bannerList.add(it.videoUrl)
//            }
        }
        return bannerList
    }

    /**
     * 创建大图数据
     */
    fun createBigIMg(list: List<WineryCoverBean>): MutableList<String> {
        val urlList = mutableListOf<String>()
        for (datum in list) {
            urlList.add(if (datum.type == 0) datum.coverUrl else datum.videoUrl)
        }
        return urlList
    }

    //查看大图
    fun seeBigImg(coverUrlList: MutableList<String>, position: Int, nameList: List<String>, descList: List<String>) {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            //查看大图
            val bundle = Bundle()
            bundle.putStringArrayList(Constants.KEY_URLS, coverUrlList as java.util.ArrayList<String>)
            bundle.putInt(Constants.KEY_INDEX, position)
            bundle.putStringArrayList(Constants.KEY_COMMENT_TITLE, if (nameList.isEmpty()) java.util.ArrayList<String>() else nameList as java.util.ArrayList<String>)
            bundle.putStringArrayList(Constants.KEY_COMMENT_CONTENT, if (descList.isEmpty()) java.util.ArrayList<String>() else descList as java.util.ArrayList<String>)
            ActivityRouter.startActivity(ActivityRouter.WINE_CELLAR.IMG_VIDEO_DETAIL, bundle)
        }
    }

    /*-----------------------新零售【经营者身份】-----------------------------------------*/
    var afterSaleListLD: MutableLiveData<BasicListBean<GoodsAfterSaleListBean>> = MutableLiveData()

    //售后详情
    var afterSaleInfoLD: MutableLiveData<AfterSalesInfoBean> = MutableLiveData()

    //订单详情
    var orderLoadDetailLD: MutableLiveData<GoodsOrderLoadDetailBean> = MutableLiveData()

    //快递信息
    var logisticsInfoLD: MutableLiveData<LogisticeInfoBean> = MutableLiveData()

    //保存发货信息
    var saveSendGoodsLD: MutableLiveData<SuccessBean> = MutableLiveData()

    val reasonStrList: ArrayList<String> = arrayListOf("大小/重量与商品描述不符", "生产日期/保质期与商品描述不符", "标签/批次/包装/成分等与商品描述不符", "商品变质/发霉/有异物", "质量问题", "其他")

    /**
     *售卖中，已下架 列表  type:类型：0售卖中，1已下架
     */
    fun requestGoodsManagerList(type: Int, dealerCode: String, page: Int, size: Int = 20) {
        WineRequestManager.getInstance().requestGoodsManagerList(type, dealerCode, page, size, goodsManagerListLD)
    }

    //经营者-售后订单列表
    fun reqGoodsAfterSaleList(dealerCode: String, page: Int) {
        WineRequestManager.getInstance().reqAfterSaleList(dealerCode, page, 20, afterSaleListLD)
    }

    // 经营者--售后单详情
    fun reqLoadAfterSaleInfo(afterSaleId: Long) {
        WineRequestManager.getInstance().reqLoadAfterSale(afterSaleId, afterSaleInfoLD)
    }

    //我的订单列表
    var orderDataLD: MutableLiveData<BasicListBean<GoodsOrderListBean>> = MutableLiveData()

    //销售订单列表
    fun reqGoodsOrderList(dealerCode: String, status: Int?, keyword: String, page: Int) {
        WineRequestManager.getInstance().reqGoodsOrderList(orderDataLD, dealerCode, status, keyword, page)
    }

    //经营者-订单详情 poId：商品订单id
    fun requestOrderDetail(poId: String) {
        WineRequestManager.getInstance().requestOrderDetail(orderLoadDetailLD, poId)
    }

    //快递信息
    fun reqLogisticsInfo(poid: String) {
        WineRequestManager.getInstance().reqLogisticsInfo(poid, logisticsInfoLD)
    }

    //快递信息
    fun reqSaveSendGoodsInfo(kuaiDiCom: String, kuaiDiNum: String, poId: Int) {
        WineRequestManager.getInstance().reqSaveSendGoodsInfo(kuaiDiCom, kuaiDiNum, poId, saveSendGoodsLD)
    }

    //店内好货
    var storeGoodsLD: MutableLiveData<BasicListBean<GoodListBean>> = MutableLiveData()

    /**
     * 店内好货，列表
     */
    fun reqStoreGoodsList(shopId: Int, page: Int) {
        WineRequestManager.getInstance().reqAllStoreGoods(storeGoodsLD, shopId, page, 10)
    }

    var goodsNumStatisticsLD: MutableLiveData<GoodsNumStatisticsBean> = MutableLiveData()

    //商品分页列表数字统计
    fun reqGoodsNumStatistics(dealerCode: String) {
        WineRequestManager.getInstance().reqGoodsNumStatistics(dealerCode, goodsNumStatisticsLD)
    }

    var changeSortLD: MutableLiveData<SuccessBean> = MutableLiveData()

    //修改排序
    fun reqGoodsModifySort(sort: String, spuId: Int) {
        WineRequestManager.getInstance().reqGoodsModifySort(sort, spuId, changeSortLD)
    }

    var allComponyLD: MutableLiveData<MutableList<AllComponyBean>> = MutableLiveData()

    //获取所有快递企业
    fun reqGetAllCompony() {
        WineRequestManager.getInstance().reqGetAllCompony(allComponyLD)
    }

    var overTimeLD: MutableLiveData<SuccessBean> = MutableLiveData()

    //经营者客户端关闭未支付超时订单
    fun reqOvertimeNoPayCancel(poId: String) {
        WineRequestManager.getInstance().reqOvertimeNoPayCancel(poId, overTimeLD)
    }

    //经营者客户端调用,超时未接收的订单修改为接收
    fun reqOvertimeReceive(poId: String) {
        WineRequestManager.getInstance().reqOvertimeReceive(poId, overTimeLD)
    }
}